/*
 * Copyright (C) 2017 Google Inc. All Rights Reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.umeox.watch.moto.launcher.adapter;

import android.annotation.SuppressLint;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.recyclerview.widget.RecyclerView;

import com.umeox.moto.common.clicklistener.OnNoDoubleClickListener;
import com.umeox.watch.moto.launcher.R;
import com.umeox.watch.moto.launcher.bean.AppInfo;

import java.util.List;

public class SecondaryMenuListAdapter extends RecyclerView.Adapter<SecondaryMenuListAdapter.Holder> {

    private final LayoutInflater mInflater;
    private List<AppInfo> mItems;
    private SecondaryMenuListAdapter.OnClickListener mOnClickListener;
    private boolean mLoop;
    private int mPaddingLeft;
    private int mPaddingTop;

    private Context mContext;

    public SecondaryMenuListAdapter(Context context, List<AppInfo> items) {
        this.mItems = items;
        mContext = context;
        mInflater = LayoutInflater.from(context);
    }

    @Override
    public SecondaryMenuListAdapter.Holder onCreateViewHolder(ViewGroup parent, int viewType) {
        final View itemView = mInflater.inflate(R.layout.item_secondary_menu_list, parent, false);
        itemView.setPadding(mPaddingLeft, mPaddingTop, 0, mPaddingTop);
        return new Holder(itemView);
    }


    @SuppressLint("ClickableViewAccessibility")
    @Override
    public void onBindViewHolder(SecondaryMenuListAdapter.Holder holder, int position) {
        if (mItems.isEmpty()) {
            return;
        }
        final int curPosition = position % mItems.size();
        final AppInfo item = mItems.get(curPosition);

        holder.bind(item);
        holder.itemView.setOnClickListener(new OnNoDoubleClickListener() {
            @Override
            protected void onNoDoubleClick(View v) {
                if (mOnClickListener != null) {
                    mOnClickListener.onPositionClick(curPosition);
                }
            }
        });
    }

    public void setLoop(boolean loop) {
        mLoop = loop;
        mPaddingLeft = mLoop ? 0 : 12;
        mPaddingTop = mLoop ? 0 : 6;
    }

    @Override
    public int getItemCount() {
        return mLoop ? Integer.MAX_VALUE : mItems.size();
    }

    public void setData(List<AppInfo> appInfoList) {
        mItems.clear();
        mItems.addAll(appInfoList);
        notifyDataSetChanged();
    }

    static class Holder extends RecyclerView.ViewHolder {
        TextView mTextView;
        ImageView mImageView;

        public Holder(final View itemView) {
            super(itemView);

            mTextView = itemView.findViewById(R.id.title_tv);
            mImageView = itemView.findViewById(R.id.icon_iv);
        }

        public void bind(AppInfo item) {
            mTextView.setText(item.appLabel);
            mImageView.setImageResource(item.appIcon);
        }
    }

    public void setOnClickListener(OnClickListener onClickListener) {
        mOnClickListener = onClickListener;
    }

    public interface OnClickListener {
        void onPositionClick(int position);
    }
}
